Testing server-side classes from client side by Nicolas FRANK (nicolas.frank@laposte.net)
(how to perform JUnit test on EJB 2.0 local interfaces).
Testing server classes can be very difficult especialy when you want to test ejbs through ejb 2.0 local interfaces.
Deploying classes on serveur side requires redeploying the all application on the server on a regular basis on the developement stage so changes could be take in account.
This framework offers the ability to upload test class definition from client side and to run it into a special Ejb Session.
In a classic J2EE architecture using an EJB container, client ejb entity calls are hidden using the Facade pattern.
Entity EJB do not have any reason to provide a remote interface every acces being do using their local interface from a session ejb deployed into the same container (Facades). Also, session EJB can expose some methods only through local interface if they don't need to be used by the client.
In this configuration it becomes difficult to build exaustif unit test cases from client side as this one can't call directly EJB entity (in the previous diagram it is impossible to test the getAccountDTO() from the client).
Tests must be instanciate from server side into the container JVM.
Nowdays, JUnit is the defacto standard as test framework. If you don't know here is what you just need to know to read further (but you should really, really check junit at www.junit.org):
- A test class must extend the junit.framework.TestCase base class.
- test methods should be public and start with 'test' (ex : public void testSomething())
- TestCase tests are executed by providing the TestCase to a TestRunner classe qui which call il its method starting with test (TestRunner.run("MyTestCase" )).
All our test classes will extend TestCases (in fact a TestCase sub-class called RemoteTestCase for an easier usage).
* How it works ?
1. The TestRunner SessionBean :
The first simple idea is to use a server side Stateless Session EJB. A client call will tell it which TestCase it must execute on the server.
Remarque : with this design the TestCase class must be available to the container.
public class TestRunnerBean implements SessionBean { public void playTestCase(String className) throws ClassNotFoundException { Class testCaseClass = Class.forName(className); // Right now we don't care how to get the TestCase final result... TestRunner.run(testCaseClass); } }
The RemoteTester framework allows to perform this kind of server tests .
As you can see the package tree is very close to junit tree :
The main package 'runner', contains the TestRunner class, which is used used on client side to run TestCase.
The server package must be deployed on... server side !, as it contains the session bean (the session remote interfaces are also deployed on client side).
The test package contains a few exemple to test the remoteTester framework.
The MySSTestCase testcase calls an EJB through it's local interface to show remoteTester main purpose.
I provide a configuration ready to run on JBoss (.ear read to be deployed and an ant script to buid it (this script can easily be adapted to your configuration).
The only jboss specific file is jboss.xml. You should replace it with your EJB container provider specific file (send me a copy if you do it so that I can make it available to everyone).
As usually you need on client side to put a jndi.properties file in the classpath in order to find the application server (can also be done programmaticly, but so boring !)